package com.example.neteasecloudmusic.ui.dicovery;

import android.content.Intent;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.neteasecloudmusic.R;
import com.example.neteasecloudmusic.bean.discoveryBean.Blocks;
import com.example.neteasecloudmusic.ui.PlayListActivity;
import com.example.neteasecloudmusic.ui.base.BaseFragment;
import com.example.neteasecloudmusic.ui.dicovery.adapter.DiscoveryAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * View layer not touch data in MVP
 */
public class DiscoveryFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, DiscoveryContract.IDiscoveryView, DiscoveryAdapter.OnItemClickListener {

    private DiscoveryAdapter discoveryAdapter;
    private DiscoveryPresenter discoveryPresenter;
//    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_discovery;
    }

    @Override
    protected void initViews() {
//        swipeRefreshLayout = find(R.id.discovery_refresh);
//        swipeRefreshLayout.setOnRefreshListener(this);
        // init Data
        List<Blocks> blocks = new ArrayList<>();
        RecyclerView recyclerView = find(R.id.discovery_recyclerview);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);

        discoveryAdapter = new DiscoveryAdapter(recyclerView, getActivity(), blocks);
        recyclerView.setAdapter(discoveryAdapter);

        discoveryAdapter.setOnItemClickListener(this);
        discoveryPresenter = new DiscoveryPresenter(this);
        discoveryPresenter.getData();
    }

    @Override
    public void onRefresh() {
//        swipeRefreshLayout.setRefreshing(false);
//        discoveryPresenter.getData();
    }

    @Override
    public void getBeanSuccess(List<Blocks> blocks) {
        discoveryAdapter.setData(blocks);
    }

    @Override
    public void getBeanFailure(Throwable throwable) {

    }

    @Override
    public void onItemClick(String id) {
        Intent intent = new Intent(getActivity(), PlayListActivity.class);
        intent.putExtra(PlayListActivity.PLAYLIST_ID, id);
        startActivity(intent);
    }
}






















